package com.meilele.controller.czh;

import com.meilele.biz.czh.ApplicationBiz;
import com.meilele.entity.czh.Application;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.meilele.util.czh.RandomimageGenerator.random;
import static com.meilele.util.czh.RandomimageGenerator.render;

@Controller
@RequestMapping("/fiveApplication")
public class ApplicationController {
    @Resource
    private ApplicationBiz applicationBiz;
    @RequestMapping("/preadd")
    public String preadd(Model model){
        List<Application>list=applicationBiz.selectAll();
        model.addAttribute("list",list);
        return "/fiveApplication/add";
    }
    @RequestMapping("/add")
    public void add(Application application){
        Date date=new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式
        String hehe = dateFormat.format(date);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date time=null;
        try {
            time= sdf.parse(sdf.format(new Date()));
            application.setApplicationTime(time);
            applicationBiz.save(application);
        } catch (ParseException e) {

            e.printStackTrace();
        }

    }
    @RequestMapping("/show")
    public void show(HttpServletResponse response, HttpSession session){
        System.out.println("jinlaile");
        ServletOutputStream out=null;
        try {
            out =response.getOutputStream();
            String randomStr=random(3);
            System.out.println(randomStr);
            session.setAttribute("yanZM",randomStr);
            render(randomStr,out,70,33);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
